package edu.cs.usask.ca.silver.plugin.rcp.dialogs.create;

import java.util.Hashtable;

import org.apache.log4j.Logger;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.ColumnViewer;
import org.eclipse.jface.viewers.EditingSupport;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TextCellEditor;

/**
 * This class allows columns from the table viewer that is associated with the
 * create new scenario dialog to be edited. The individual columns have this
 * attribute assigned to them thus allowing the cells within the column to be
 * edited
 * 
 * similar to "AddScenarioEditingSupport.java"
 * 
 * @author Alan Yeung, Mike Taylor
 * 
 */
public class CreateScenarioEditingSupport extends EditingSupport {

	private final CellEditor editor;
	private final String columnName;
	private final CreateScenarioDialog createNewScenarioDialog;
	private static Logger logger = Logger.getLogger(CreateScenarioEditingSupport.class);

	/**
	 * Constructor
	 * 
	 * @param viewer
	 *            the column that is editable
	 * @param columnName
	 *            take in the column name that cell belong to that has been
	 *            edited
	 * @param addNewScenarioDialog
	 *            takes in the dialog box to watch for cell selection and
	 *            changes
	 */
	public CreateScenarioEditingSupport(ColumnViewer viewer, String columnName,
			CreateScenarioDialog createNewScenarioDialog) {
		super(viewer);
		editor = new TextCellEditor(((TableViewer) viewer).getTable());
		this.columnName = columnName;
		this.createNewScenarioDialog = createNewScenarioDialog;
	}

	@Override
	protected boolean canEdit(Object element) {
		return true;
	}

	@Override
	protected CellEditor getCellEditor(Object element) {
		return editor;
	}

	/**
	 * 
	 * return the string that is contained in the hashtable using the column
	 * name as a key within the hashtable
	 */

	@Override
	protected Object getValue(Object element) {
		// here element is a hash-table
		Object value = ((Hashtable<String, String>) element).get(columnName);
		return value;
	}

	/**
	 * a method that changes the value in the array of hashtables associated
	 * with the cell that was edited
	 */
	@SuppressWarnings("unchecked")
	@Override
	protected void setValue(Object element, Object value) {
		// TODO Auto-generated method stub
		System.out
				.println("Entering check the asserts in newScenarionEditingSupport setValue");
		assert (element instanceof Hashtable);
		assert (value instanceof String);
		// logger.info("Leaving check the asserts in newScenarionEditingSupport setValue");
		// logger.info("**** " + value.toString() + " ****");
		//
		// // update the model object editor's data about the parameter
		// logger.info("This is the old value before changing : " +
		// ((Hashtable<String, String>) element).get(columnName) );
		// logger.info("#################################################");
		//
		// //THIS IS GOING TO BE FROM THE HASHTABLE
		//
		// //String parameterName = ((Hashtable<String, String>)
		// element).get(NAME);
		/* editing the data in the hashtable for a given parameter */
		((Hashtable<String, String>) element).remove(columnName);
		((Hashtable<String, String>) element).put(columnName, (String) value);

		// NewScenarioTableContentProvider.fakeParams.get(columnName).remove(parameterName);
		// _moEditor._params.get(columnName).put(parameterName, (String)value);
		// /ProjectsViewer.adapterFactory.setParameterUnits((ParameterID)element,
		// (String)value);
		logger.info("This is the new value : "
				+ ((Hashtable<String, String>) element).get(columnName));

		// update the content provider's data about the parameter
		// UNKNOWN IF THIS IS NEEDED
		// ((Hashtable<String, String>) element).remove(columnName);
		// ((Hashtable<String, String>) element).put(columnName, (String)value);
		getViewer().update(element, null);

	}

} /* end of class */
